<?php

namespace App\Http\Controllers\admin;

use App\Models\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Intervention\Image\ImageManagerStatic as Image;

class SuperController extends Controller
{
    //
    public function index()
    {
        $admin=new Admin();
        $list=$admin->get();
        return view('admin.super.superlist',['list'=>$list]);
    }
    public function show()
    {

    }
    public function create()
    {
        return view('admin.super.addsuper');
    }
    public function store(Request $request)
    {
        $admin=new Admin();
//        return $request;
        $data=$request->except('_token');
        $admin->username=$data['username'];
        $admin->password=$data['password'];
        //ip固定没有取  上次登录时间是在登录时就更新的
        $admin->ip=$data['ip'];
        $admin->status=$data['status'];
        $admin->level=$data['level'];
        $row= $admin->save();
        if($row)
        {
            return json_encode(true);
        }else{
            return json_encode(false);
        }


    }

    public function edit($id)
    {
        $admin=new Admin();
        $list=$admin->where('id',$id)->first();
//        dd($list);
        return view('admin.super.updatesuper',['list'=>$list]);
    }

    public function update( Request $request,$id)
    {
        $admin=new Admin();
        $data=$request->only('username','password','ip','level','status');
        $data['password']=md5($data['password']);
        $row=$admin->where('id',$id)->update($data);
        $list=$admin->get();
        if($row>0)
        {
            return view('admin/super/superlist',['list'=>$list]);
        }else{
            return view('admin/super/superlist',['list'=>$list]);
        }

    }

    public function delete(Request $request)
    {
        $admin=new Admin();
        $id=$request->id;
        $row=$admin->where('id',$id)->delete();
        if($row>0)
        {
            return json_encode('true');
        }else
        {
            return json_encode('false');
        }
    }
    public function setstatus(Request $request)
    {
        $admin=new Admin();
        $id=$request->id;
        $status=$request->status;
//        return json_encode('111');
        $row=$admin->where('id',$id)->update(['status'=>$status]);
       if($row>0)
       {
           return json_encode('true');
       }else{
           return json_encode('false');
           }
    }

    public function editLogo(Request $request)
    {
        $id = $request->input('id');
        $admin=new Admin();
//        dd($request);
        $data=$admin->where('id',$id)->first();
        $data->pic = $this->file($request);
//        dd($data->pic);
        $bool = $data->save();
        if($bool){
            return back();
        }

    }

    public function file(Request $request)
    {
//        dd($request->hasFile('photo'));
        if ($request->hasFile('photo')) {
            if ($request->file('photo')->isValid()) {
                $file = $request->file('photo');
                $ext = $file->getClientOriginalExtension();
                $picname = time() . rand(1000, 9999) . '.' . $ext;
//                dd($picname);
                $img = Image::make($file);
                $imgs = Admin::find($request->id)->pic;
                if(!empty($imgs) && file_exists('./upload/admin/'.$imgs)){
                    unlink('./upload/admin/'.$imgs);
                }
                $img->save('./upload/admin/'.$picname);
                session('storelist')['pic']=$picname;
                return $picname;

            }
        }
    }

}
